package com.school.aftersalemanagement.callback.impl;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import com.school.aftersalemanagement.callback.StringCallback;
import com.school.aftersalemanagement.entity.Result;
import com.school.aftersalemanagement.entity.User;
import com.school.aftersalemanagement.global.BaseActivity;
import com.school.aftersalemanagement.global.UserConfig;
import com.zhy.http.okhttp.request.RequestCall;

/**
 * Created by winkyle on 2017/11/8.
 */

public class UserCallback extends StringCallback{


    public UserCallback(RequestCall requestCall, Handler handler, int messageType, Context context, boolean cycle) {
        super(requestCall, handler, messageType, context, cycle);
    }

    @Override
    public void onResponse(String response, int id) {
        Log.d(requestCall.getClass().getSimpleName()+"response", response);
        Result result = getResult(response);
        if(result.getCode()==0){
            showShort(result.getMsg());
        }else if(result.getCode()==1){
            User us = gson.fromJson(result.getBody(),User.class);
            //适配

            setUser(us);
            //获取到用户信息
            String userinfo = gson.toJson(us);
            UserConfig.getInstance(context).setUser(userinfo);
        }
    }

    public void logout() {
        setUser(null);
    }

    private void setUser(User user){
        handler.obtainMessage(messageType,user).sendToTarget();
    }
}
